/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JLinkContainerPanel.java
 *
 * Created on 2010-5-31, 8:24:44
 */

package cn.ekuma.epos.analysis.businesspartner;

import cn.ekuma.epos.analysis.businesspartner.link.JOrderLinePanel;
import cn.ekuma.epos.datalogic.I_DataLogicCustomers;
import cn.ekuma.epos.datalogic.I_DataLogicERP;
import cn.ekuma.epos.datalogic.I_DataLogicSales;
import cn.ekuma.epos.datalogic.I_DataLogicSystem;
import cn.ekuma.epos.module.ERP_UI_Swing_Model;
import cn.ekuma.epos.order.model.OrderRelationTableModel;
import cn.ekuma.epos.qkorder.event.OrderChangedEvent;
import cn.ekuma.epos.qkorder.event.OrderChangedListener;
import cn.ekuma.epos.qkorder.link.JOrderLinkPanel;
import cn.ekuma.epos.qkorder.link.JOrderPaymentsPanel;
import cn.ekuma.epos.qkorder.link.JOrderStatesPanel;
import cn.ekuma.rabbit.spi.I_ModelPart;
import cn.ekuma.util.SwingUtil;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.pos.base.AppLocal;
import com.openbravo.pos.base.AppView;
import com.openbravo.bean.erp.Order;

/**
 *
 * @author Administrator
 */
public class JLinkContainerPanel extends javax.swing.JPanel implements OrderInfoChangedListener{
    public Order currentOrder;
    I_DataLogicERP dlSales;
    I_DataLogicSystem dlSystem;
    AnalysisModelManager manager;
    OrderRelationTableModel refTableModel;
	private AppView app;
	I_DataLogicCustomers dlCustomers;
	
    /** Creates new form JLinkContainerPanel */
    public JLinkContainerPanel(AnalysisModelManager manager,AppView app,I_DataLogicERP dlSales, I_DataLogicSystem dlSystem,I_DataLogicCustomers dlCustomers) {
        initComponents();
        this.manager=manager;
        this.app=app;
        this.dlCustomers=dlCustomers;
        this.dlSales=dlSales;
        this.dlSystem=dlSystem;
        
        JOrderLinePanel orderLinePanel=new JOrderLinePanel(app,dlSystem);
        jTabbedPane1.addTab(AppLocal.getIntString("label.orderLine"), orderLinePanel);
        addOrderChangedListener(orderLinePanel);

        JOrderLinkPanel orderLinkPanel=new JOrderLinkPanel(app, dlSales, dlSystem,dlCustomers);
        jTabbedPane1.addTab(AppLocal.getIntString("label.OrderLinkPanel.name"), orderLinkPanel);
        addOrderChangedListener(orderLinkPanel);

        JOrderStatesPanel orderStatesPanel=new JOrderStatesPanel(null,null);
        jTabbedPane1.addTab(AppLocal.getIntString("label.orderStates"),orderStatesPanel);
        addOrderChangedListener(orderStatesPanel);

        JOrderPaymentsPanel orderPaymentPanel=new JOrderPaymentsPanel();
        jTabbedPane1.addTab(AppLocal.getIntString("Label.Payment"),orderPaymentPanel);
        addOrderChangedListener(orderPaymentPanel);

        refTableModel=new OrderRelationTableModel();
        jXTable1.setColumnControlVisible(true);
        jXTable1.setModel(refTableModel);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        jXTable1 = new org.jdesktop.swingx.JXTable();

        setLayout(new java.awt.BorderLayout());

        jXTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jXTable1);

        jTabbedPane1.addTab(AppLocal.getIntString("label.orderRefBy"), jScrollPane1); // NOI18N

        add(jTabbedPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private org.jdesktop.swingx.JXTable jXTable1;
    // End of variables declaration//GEN-END:variables

    public void addOrderChangedListener(OrderChangedListener l){
        listenerList.add(OrderChangedListener.class, l);
    }

    public void removeOrderChangeListener(OrderChangedListener l){
       listenerList.remove(OrderChangedListener.class, l);
   }

    public OrderChangedListener[] getOrderChangedListeners() {
        return (OrderChangedListener[])listenerList.getListeners(OrderChangedListener.class);
    }

     public void fireOrderChanged() {
        fireOrderChanged(new OrderChangedEvent(currentOrder,false));
    }

    public void fireOrderChanged(OrderChangedEvent e) {
	// Guaranteed to return a non-null array
	Object[] listeners = listenerList.getListenerList();
	// Process the listeners last to first, notifying
	// those that are interested in this event
	for (int i = listeners.length-2; i>=0; i-=2) {
	    if (listeners[i]==OrderChangedListener.class) {
		((OrderChangedListener)listeners[i+1]).orderChanged(e);
	    }
	}
    }

    public void orderInfoChanged(Object src, String orderId) {
       if(orderId!=null){
            try {
                currentOrder= dlSales.loadOrder(orderId);
            } catch (BasicException ex) {
                 MessageInf msg = new MessageInf(MessageInf.SGN_WARNING, AppLocal.getIntString("message.cannotLoadOrder"),ex);
                 msg.show(this);
                 currentOrder=null;
            }
            if (currentOrder != null)  {
                this.fireOrderChanged();
                refTableModel.resert(manager.getOrderRelationListToByOrderId(currentOrder.getId()));
                SwingUtil.PreferredJXTableColumn(jXTable1);
         }
       }
    }
}
